<template>
    <div>
        <!-- 查询条件开始 -->
        <el-form ref="search" :model="search" :inline="true" label-width="88px">
            <el-form-item label="公告标题" prop="noticeTitle">
                <el-input
                        v-model="search.noticeTitle"
                        placeholder="请输入通知公告标题"
                        clearable
                        size="small"
                        style="width:240px"/>
            </el-form-item>
            <el-form-item label="发布者" prop="createBy">
                <el-input
                        v-model="search.createBy"
                        placeholder="请输入发布者"
                        clearable
                        size="small"
                        style="width:240px"/>
            </el-form-item>
            <el-form-item label="类型" prop="noticeType">
                <el-select
                        v-model="search.noticeType"
                        placeholder="类型"
                        clearable
                        size="small"
                        style="width:240px">
                    <el-option
                            v-for="dict in noticeTypeOptions"
                            :key="dict.dictValue"
                            :label="dict.dictLabel"
                            :value="dict.dictValue"/>
                </el-select>
            </el-form-item>
            <el-form-item label="状态" prop="status">
                <el-select
                        v-model="search.status"
                        placeholder="可用状态"
                        clearable
                        size="small"
                        style="width:240px">
                    <el-option
                            v-for="dict in statusOptions"
                            :key="dict.dictValue"
                            :label="dict.dictLabel"
                            :value="dict.dictValue"/>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button type="primary" icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
        <!-- 查询条件结束 -->

        <!-- 表格工具按钮开始 -->
        <el-row :gutter="10" style="margin-bottom: 8px;">
            <el-col :span="1.5">
                <el-button type="primary" icon="el-icon-plus" size="mini" @click="addNotice">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete">删除</el-button>
            </el-col>
        </el-row>
        <!-- 表格工具按钮结束 -->

        <!-- 数据表格开始 -->
        <el-table  border :data="noticeTableList" @selection-change="handleSelectionChnage">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="公告ID" align="center" prop="noticeId" />
            <el-table-column label="公告标题" align="center" prop="noticeTitle" />
            <el-table-column label="公告类型" prop="noticeType" align="center" :formatter="noticeTypeFormatter" />
            <el-table-column label="公告状态" prop="status" align="center" :formatter="statusFormatter" />
            <el-table-column label="发布者" align="center" prop="createBy" />
            <el-table-column label="创建时间" align="center" prop="createTime" />
            <el-table-column label="操作" align="center">
                <template slot-scope="scope">
                    <el-button type="text" icon="el-icon-edit" size="mini" @click="handleUpdate(scope.row)">修改</el-button>
                    <el-button type="text" icon="el-icon-delete" size="mini" @click="handleDelete(scope.row)">删除</el-button>
                    <el-button type="text" icon="el-icon-view" size="mini" @click="handleView(scope.row)">查看</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 数据表格结束 -->

        <!--  分页-->
        <el-pagination
                v-show="page.total>0"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="page.current"
                :page-sizes="[4, 5, 6]"
                :page-size="page.size"
                layout="total, sizes, prev, pager, next, jumper"
                :total="page.total">
        </el-pagination>
        <!--  分页结束-->

        <!-- 添加修改弹出层开始 -->
        <el-dialog
                :title="title"
                :visible.sync="open"
                width="800px"
                center
                append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="100px">
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="标题" prop="noticeTitle">
                            <el-input v-model="form.noticeTitle" placeholder="请输入通知公告标题" clearable size="small" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="状态" prop="status">
                            <el-radio-group v-model="form.status">
                                <el-radio
                                        v-for="dict in statusOptions"
                                        :key="dict.dictValue"
                                        :label="dict.dictValue"
                                        :value="dict.dictValue">{{ dict.dictLabel }}</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="类型" prop="noticeType">
                            <el-radio-group v-model="form.noticeType">
                                <el-radio
                                        v-for="dict in noticeTypeOptions"
                                        :key="dict.dictValue"
                                        :label="dict.dictValue"
                                        :value="dict.dictValue">{{ dict.dictLabel }}</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="公告内容">
                            <mavon-editor
                                    ref="noticeContent"
                                    v-model="form.noticeContent"
                                    height="300px"
                                    :options="{hideModeSwitch:true,previewStyle:'tab'}"/>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="备注" prop="remark">
                            <el-input v-model="form.remark" placeholder="请输入备注" clearable size="small" />
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </span>
        </el-dialog>
        <!-- 添加修改弹出层结束 -->

        <!--公告内容 弹出层开始 -->
        <el-dialog
                :title="title"
                :visible.sync="noticeContentOpen"
                width="800px"
                center
                append-to-body>
            <mavon-editor ref="noticeContent" v-model="noticeContent" :aria-disabled="true" :options="{hideModeSwitch:true,previewStyle:'tab'}" />
        </el-dialog>
        <!-- 公告内容弹出层结束 -->

    </div>
</template>

<script>
    export default {
        name: "Notice",
        data(){
            return{
                //条件
                search:{},
                //禁用按钮 批量删除
                multiple:true,
                //表格数据
                noticeTableList:[],
                //删除多选框
                noticesId: [],
                //状态类型
                statusOptions:[],
                //通知类型
                noticeTypeOptions:[],
                //分页
                page:{
                    current:1,
                    size:5,
                    total:10
                },
                //表单数据
                form:{},
                //标题
                title:'',
                //添加修改对话框
                open:false,
                // 表单校验
                rules: {
                    noticeTitle: [
                        { required: true, message: '通知公告标题不能为空', trigger: 'blur' }
                    ]
                },
                //查询看通知公告弹出层
                noticeContentOpen:false,
                // 查看内容
                noticeContent: undefined
            }
        },
        methods:{
            //条件查询
            handleQuery(){
                this.page.current=1;
                this.initNoticeTable();
            },
            //清空查询表单
            resetQuery(){
                this.search={};
            },
            //分页 每页显示的数量
            handleSizeChange(val){
                this.page.size=val;
                this.initNoticeTable();
            },
            //分页 显示的当前页数
            handleCurrentChange(val){
                this.page.current=val;
                this.initNoticeTable();
            },
            //添加通知
            addNotice(){
                if (this.$refs.form != undefined) {
                    this.$refs.form.clearValidate();
                }
                this.title="添加通知公告信息";
                this.form={noticeType:'0',status:'0'};
                this.open=true;
            },
            //修改
            handleUpdate(row){
                if (this.$refs.form != undefined) {
                    this.$refs.form.clearValidate();
                }
                this.title="修改通知公告信息";
                this.form =JSON.parse(JSON.stringify(row));
                this.form.noticeId=row.noticeId;
                this.open=true;
            },
            //提交表单
            handleSubmit(){
                this.$refs['form'].validate((valid) => {
                    if (valid) {
                        this.$axios.post("system/api/notice/saveOrUpdateNotice",this.form).then(r=>{
                            if (r.data.t){
                                this.$message.success(r.data.msg);
                            }else {
                                this.$message.error("操作失败");
                            }
                            this.initNoticeTable();
                            this.open=false;
                            this.title='';
                            this.form={};
                        })
                    }
                });
            },
            //取消
            cancel(){
                this.form={};
                this.open=false;
            },
            //查看
            handleView(row){
                this.title = row.noticeTitle
                this.noticeContentOpen = true
                this.noticeContent = row.noticeContent
            },
            //多选框触发事件
            handleSelectionChnage(val){
                this.noticesId=val.map(item=>item.noticeId);
                this.multiple = !val.length;
            },
            //删除 批量删除
            handleDelete(row){
                const noticeIds=row.noticeId || this.noticesId;
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$axios.delete("system/api/notice/delNotice/"+noticeIds).then(r=>{
                        if (r.data.t){
                            this.$message.success(r.data.msg);
                        }else {
                            this.$message.error("操作失败");
                        }
                        this.initNoticeTable();
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            //加载通知表格数据
            initNoticeTable(){
                this.$axios.post("system/api/notice/getAllList/"+this.page.current+"/"+this.page.size,this.search).then(r=>{
                    this.noticeTableList=r.data.t.records;
                    this.page.total=r.data.t.total;
                })
            },

            //加载系统状态字典
            initNStatus(){
                this.$axios.get("system/api/dict/data/findByType/sys_normal_disable").then(r=>{
                    this.statusOptions=r.data.t;
                })
            },
            //系统状态格式化解析
            statusFormatter(row){
                var val = row.status;
                return this.formatDict(this.statusOptions,val);
            },
            //加载通知字典数据
            initNNotice(){
                this.$axios.get("system/api/dict/data/findByType/sys_notice_type").then(r=>{
                    this.noticeTypeOptions=r.data.t;
                })
            },
            //通知状态解析
            noticeTypeFormatter(row){
                var val = row.noticeType;
                return this.formatDict(this.noticeTypeOptions,val);
            },

        },
        created() {
            //加载状态字典
            this.initNStatus();
            //加载通知字典数据
            this.initNNotice();
            //加载通知表格数据
            this.initNoticeTable();
        }
    }
</script>
<style scoped>
</style>
